﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Sales;
namespace NotebookS2
{
    public partial class M_SETUPRICE : RibbonForm
    {
        public M_SETUPRICE()
        {
            InitializeComponent();
        }
        public DataTable Prices { get; set; }
        public string ConnectionString { get; set; }

        void BindPrices(string sPriceList)
        {
            PriceManager oManager = new PriceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();


            DataTable _table = oManager.GetPriceListByPriceCode(sPriceList);
            oManager.Close();

            this.Prices = _table;
            gridControl1.DataSource = this.Prices;
            gridControl1.Refresh();
        }

        void BindPriceList()
        {
            PriceManager oManager = new PriceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable _table = oManager.GetPriceList();

            oManager.Close();

           
            cboPriceList.DisplayMember = "PRICELISTNAME";
            cboPriceList.ValueMember = "PRICELISTCODE";
            cboPriceList.DataSource = _table;
            cboPriceList.Refresh();
            
        }
        private void M_SETUPRICE_Load(object sender, EventArgs e)
        {
            BindPriceList();
        }

        private void cboPriceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPrices(cboPriceList.SelectedValue.ToString());

            PriceManager oManager = new PriceManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataRow oRow = oManager.GetPriceList(cboPriceList.SelectedValue.ToString());

            oManager.Close();

            txtRemarks.Text = oRow["Remarks"].ToString();
            txtBasePrice.Text = oRow["BasePriceListCode"].ToString();
            txtFactor.Text = oRow["Factor"].ToString();
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                PriceManager oManager = new PriceManager();
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                foreach (DataRow oRow in this.Prices.Rows)
                {
                    PriceUnitLineItems oUnit = new PriceUnitLineItems();
                    if (oRow.RowState == DataRowState.Added)
                    {
                        oUnit.PriceCode = cboPriceList.SelectedValue.ToString();
                        oUnit.PriceName = cboPriceList.Text;
                        oUnit.ItemCode = oRow["ITEMCODE"].ToString();
                        oUnit.ItemName = oRow["ITEMNAME"].ToString();
                        oUnit.Price = Convert.ToDouble(oRow["PRICE"]);

                        oManager.AddPrice(oUnit);
                    }
                    else if (oRow.RowState == DataRowState.Modified)
                    {
                        oUnit.PriceCode = cboPriceList.SelectedValue.ToString();
                        oUnit.PriceName = cboPriceList.Text;
                        oUnit.ItemCode = oRow["ITEMCODE"].ToString();
                        oUnit.ItemName = oRow["ITEMNAME"].ToString();
                        oUnit.Price = Convert.ToDouble(oRow["PRICE"]);

                        oManager.UpdatePrice(oUnit);
                    }
                }
                oManager.Close();
                btnUpdate.Enabled = false;
                MessageBox.Show("Warehouse is successfully updated!!!", "Warehouse", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error thrown by the sytem. " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnUpdatePriceList_Click(object sender, EventArgs e)
        {
            PriceUnit oUnit = new PriceUnit();
            btnUpdate.Enabled = false;
            oUnit.PriceListCode = cboPriceList.SelectedValue.ToString();
            oUnit.PriceListName = cboPriceList.Text;
            oUnit.Remarks = txtRemarks.Text;
            oUnit.BasePriceList = txtBasePrice.Text;
            oUnit.Factor = txtFactor.Text;

            PriceManager oManager = new PriceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            oManager.UpdatePriceList(oUnit);
            oManager.Close();

            BindPrices(cboPriceList.SelectedValue.ToString());

            MessageBox.Show("Updated successfully!!!", "Price List", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnAddPriceList_Click(object sender, EventArgs e)
        {
            M_ADDPRICELIST oForm = new M_ADDPRICELIST();
            
            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            if (oForm.PriceListCode != null)
            {
                if (oForm.PriceListCode != "")
                {
                    BindPriceList();

                    cboPriceList.SelectedValue = oForm.PriceListCode;
                    cboPriceList.Refresh();
                }
            }
            
        }

        private void txtBasePrice_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_PLN oForm = new FND_PLN();

            oForm.ConnectionString = this.ConnectionString;

            oForm.ShowDialog();

            if (oForm.PriceListCode != null)
            {
                txtBasePrice.Text = oForm.PriceListCode;
            }
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            btnUpdate.Enabled = true;
        }

        private void txtBasePrice_EditValueChanged(object sender, EventArgs e)
        {
            if (txtBasePrice.Text != "")
            {
                btnUpdatePriceList.Enabled = true;
            }
            else
            {
                btnUpdatePriceList.Enabled = false;
            }
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            M_ADDITEM_IN_PL oForm = new M_ADDITEM_IN_PL();
            oForm.ConnectionString = this.ConnectionString;
            oForm.PriceListCode = cboPriceList.SelectedValue.ToString();
            oForm.PriceListName = cboPriceList.Text;
            oForm.ShowDialog();


            BindPrices(cboPriceList.SelectedValue.ToString());

        }
    }
}
